package com.bluegalaxy.sitescore.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bluegalaxy.sitescore.entity.*;
import com.bluegalaxy.sitescore.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/examine")
public class ExamineController extends BaseController {

    @Autowired
    private ITbSiteInfoService siteInfoService;

    @Autowired
    private ITBSelectDangerousService selectDangerousService;

    @Autowired
    private TbSiteSourceService siteSourceService;

    @Autowired
    private ITbDangerousService tbDangerousService;

    @Autowired
    private IGroundWaterService groundWaterService;

    @Autowired
    private SurfaceWaterGWService surfaceWaterGWService;

    @Autowired
    private SurfaceWaterService surfaceWaterService;

    @Autowired
    private TbSiteSourceService tbSiteSourceService;

    @Autowired
    private ITbSoilExposureService iTbSoilExposureService;

    @Autowired
    private ITbAirScoreService iTbAirScoreService;

    @Autowired
    private ITbMessageService messageService;

    @com.baomidou.kisso.annotation.Permission("2004")
    @RequestMapping("/list")
    public String list(Model model) {
        /*model.addAttribute("provinceId",province);
        model.addAttribute("cityId",city);
        model.addAttribute("province",iHatProvinceService.getProvinceService(province));
        model.addAttribute("city",iHatCityService.getCity(province,city));*/
        return "/examine/list";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/details/{siteId}")
    public String edit(@PathVariable Long siteId,Model model) {
        TbSiteInfo tbSiteInfo=siteInfoService.getSiteInfoById(siteId);
        model.addAttribute("province", tbSiteInfo.getProvince());
        model.addAttribute("city", tbSiteInfo.getCity());
        model.addAttribute("site", tbSiteInfo);
        return "/examine/details";
    }

    @ResponseBody
    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/getSiteList")
    public String getSiteList() {
        Page<TbSiteInfo> page = getPage();
     /*   List<TbSiteInfo> tbSiteInfoList = siteInfoService.getSiteInfoExamine(page);
        page.setRecords(tbSiteInfoList);
        page.setTotal(tbSiteInfoList.size());*/
        page  =  siteInfoService.getSiteInfoExamine(page);
        return jsonPage(page);
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/source/{siteId}/{id}")
    public String source(@PathVariable Integer siteId, @PathVariable Integer id, Model model) {
        TbSiteSource tbSiteSource = new TbSiteSource();
        List<SiteSource> siteSourceList = siteSourceService.getTbSiteSource(siteId);
        if(siteSourceList.isEmpty()){
            model.addAttribute("source",tbSiteSource);
        }else {
            tbSiteSource = siteSourceService.getTbSiteSourceById(siteSourceList.get(0).getId());
            model.addAttribute("source",tbSiteSource);
        }
       /* if(id==0){
            model.addAttribute("source",tbSiteSource);
        }else{
            tbSiteSource = siteSourceService.getTbSiteSourceById(id);
           // System.out.println(tbSiteSource.getSource_type());
            model.addAttribute("source",tbSiteSource);
        }*/
       // model.addAttribute("dangerousList",toJson(dangerousService.getDangerousAll()));
        model.addAttribute("siteId", siteId);
        model.addAttribute("dangerousSelect",toJson(selectDangerousService.getDangerousBysid(siteId,1)));
        //  model.addAttribute("siteSource", toJson(siteSourceService.getTbSiteSource(siteId)));
        return "/examine/source";
    }


    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/ground/{siteId}/{gid}")
    public String ground(Model model,@PathVariable int siteId,@PathVariable int gid){
        int sourceId = siteId;
        //  List<SelectDangerous> selectDangerousList = itbSelectDangerousService
        List<SelectDangerous> selectDangerousList = selectDangerousService.getDangerousBysid(sourceId,2);
        if (!selectDangerousList.isEmpty()){
            model.addAttribute("sdList",toJson(selectDangerousList));
        }else{
            List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected2(sourceId);
            model.addAttribute("sdList",toJson(dangerousList));
        }
        if(gid!=0){
            model.addAttribute("groundWater",groundWaterService.getGroundWaterById(gid,sourceId));
        }
        model.addAttribute("gid",gid);
        model.addAttribute("siteId",siteId);
        model.addAttribute("gList",toJson(groundWaterService.getGroundWaterList(sourceId)));
        return "/examine/ground";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/surfaceAdd/{siteId}/{id}")
    public String surfaceAdd(Model model,@PathVariable  int id,@PathVariable int siteId) {
        int sourceId = siteId;

        if(id!=0){
            SurfaceWaterOL surfaceWaterOL = surfaceWaterService.getSurfaceWaterById(id);
            if(surfaceWaterOL == null){
                id=0;
            }
            model.addAttribute("surfaceWater",surfaceWaterOL);
        }

        //model.addAttribute("sWList",toJson(surfaceWaterService.getSWOverLandList()));
        List<SelectDangerous> selectDangerousList = selectDangerousService.getDangerousBysid(sourceId,3);
        if (!selectDangerousList.isEmpty()){
            model.addAttribute("sdList",toJson(selectDangerousList));
        }else{
            List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected2(sourceId);
            model.addAttribute("sdList",toJson(dangerousList));
        }

        //    System.out.println(selectDangerousList.get(0).getChemname());
        List<TbScore> tbScoreList = surfaceWaterService.getSWOverLandList(sourceId);
        model.addAttribute("sWList",toJson(tbScoreList));
//        model.addAttribute("sdList",toJson(selectDangerousList));
        model.addAttribute("sIndex",0);
        model.addAttribute("sId",id);
        return "/examine/surfaceAdd";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/surfaceAdd2/{siteId}/{id}")
    public String surfaceAdd2(Model model,@PathVariable  int siteId,@PathVariable  int id) {
        int sourceId = siteId ;
        if(id!=0){
            SurfaceWaterGW surfaceWaterGW = surfaceWaterGWService.getGroundSurfaceById(id);
            if(surfaceWaterGW == null){
                id = 0;
            }
            model.addAttribute("surfaceWaterGW",surfaceWaterGW);
        }
        List<SelectDangerous> selectDangerousList = selectDangerousService.getDangerousBysid(sourceId,4);
//        model.addAttribute("sdList",toJson(selectDangerousList));
        if (!selectDangerousList.isEmpty()){
            model.addAttribute("sdList",toJson(selectDangerousList));
        }else{
            List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected2(sourceId);
            model.addAttribute("sdList",toJson(dangerousList));
        }
        List<TbScore> tbScoreList =surfaceWaterGWService.getSWGroundList(sourceId);
        model.addAttribute("sIndex",1);
        model.addAttribute("gId",id);
        model.addAttribute("gWList",toJson(tbScoreList));
        return "/examine/surfaceAdd2";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/surfaceIndex/{siteId}/{sid}/{gid}/{sIndex}")
    public String surfaceIndex(Model model,@PathVariable int siteId,@PathVariable  int sid,@PathVariable  int gid,@PathVariable  int sIndex) {
       /* if(id!=0&sIndex==0){
            SurfaceWaterOL surfaceWaterOL = surfaceWaterService.getSurfaceWaterById(id);
            model.addAttribute("surfaceWater",surfaceWaterOL);
        }*/
        List<TbScore> sWList = surfaceWaterService.getSWOverLandList(siteId);
        List<TbScore> gWList = surfaceWaterGWService.getSWGroundList(siteId);
        if(surfaceWaterService.getSurfaceWaterById(sid)==null){
            sid = 0;
        }
        if(surfaceWaterGWService.getGroundSurfaceById(gid)==null){
            gid = 0;
        }
        model.addAttribute("sWList",toJson(sWList));
        model.addAttribute("gWList",toJson(gWList));
        model.addAttribute("sIndex",sIndex);
        model.addAttribute("sId",sid);
        model.addAttribute("gId",gid);
        model.addAttribute("siteId",siteId);
        return "/examine/surfaceIndex";
    }


    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/soil/{siteId}")
    public String soil(Model model,@PathVariable int siteId) {
        int id =siteId;
        if(iTbSoilExposureService.getCountById(id)!=0) {
            model.addAttribute("soil", iTbSoilExposureService.selectById(id));
        }else {
            model.addAttribute("soil", new TbSoilExposure());
        }
        List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected(id);
        model.addAttribute("dangerouSelect",toJson(dangerousList));
        model.addAttribute("observable",tbSiteSourceService.isObservable(id));
        model.addAttribute("siteId",siteId);
        return "/examine/soil";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/air/{siteId}")
    public String air(Model model,@PathVariable int siteId) {
        int id =siteId;
        List<SelectDangerous> selectDangerousList = selectDangerousService.getDangerousBysid(id,6);
        if (!selectDangerousList.isEmpty()){
            model.addAttribute("dangerouSelect",toJson(selectDangerousList));
        }else{
            List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected2(id);
            model.addAttribute("dangerouSelect",toJson(dangerousList));
        }
        if (iTbAirScoreService.isAirExist(id)){
            model.addAttribute("air",iTbAirScoreService.getAirBySid(id));
        }else{
            model.addAttribute("air",new TbAirScore());
        }
        model.addAttribute("siteId",siteId);
        return "/examine/air";
    }


    @com.baomidou.kisso.annotation.Permission("2004")
    @RequestMapping(value = "/examine",method = RequestMethod.POST)
    public  @ResponseBody String examine(@RequestParam("id") Integer id,@RequestParam("idea")String idea,
                                         @RequestParam("flag")boolean flag , Model model){
        boolean result = false;
        if(siteInfoService.examineSiteInfoById(id,idea,flag)&&messageService.insertMessage(getCurrentUserId(),id)){
            result = true;
        }
        return  callbackSuccess(result);
    }

    @ResponseBody
    @com.baomidou.kisso.annotation.Permission("2004")
    @RequestMapping(value = "/querySiteList",method = RequestMethod.GET)
    public String getSiteListByIdAndName(Integer province,Integer  city,String sid,String name) {
        Page<TbSiteInfo> page = getPage();
      //  System.out.println("------"+sid+","+name+","+province+","+city);
       // page = siteInfoService.getSiteInfoListByIdAndName(page,provinceStr,cityStr,sid,name);
        page = siteInfoService.getSiteInfoByIdAndName(page,sid,name);
        return jsonPage(page);
    }


}
